package com.ww.basic.types

object EitherTest {
  def compute(input: Int) =
    if (input > 0)
      Right(math.sqrt(input))
    else
      Left("Error computing, invalid input")

  def displayResult(result: Either[String, Double]): Unit = {
    println(s"Raw: $result")
    result match {
      case Right(value) => println(s"result $value")
      case Left(err) => println(s"Error: $err")
    }
  }

  def main(args: Array[String]): Unit = {
    /**
     * 打印结果
     * Right(1.4142135623730951)
     * Left(Error computing, invalid input)
     */
    println(compute(2))
    println(compute(-1))

    /**
     * 打印结果
     * Raw: Right(1.4142135623730951)
     * result 1.4142135623730951
     * Raw: Left(Error computing, invalid input)
     * Error: Error computing, invalid input
     */
    displayResult(compute(2))
    displayResult(compute(-1))
  }
}